/*
 * 字典类型管理 Mock 数据
 * 基于蒜苔系统字典管理设计文档，提供完整的字典类型测试数据
 */

import type { SysDictType } from './types';

// 字典类型 Mock 数据 - 基于设计文档的业务场景
const mockDictTypes: SysDictType[] = [
  // 系统内置字典类型 - 用户相关
  {
    id: 1,
    dictName: '用户状态',
    dictType: 'user_status',
    dictDesc: '用户账户状态字典，用于标识用户账户的当前状态（正常、禁用、待审核）',
    status: 1,
    isSystem: 1,
    sortOrder: 1,
    createdBy: 1,
    updatedBy: 1,
    deleted: 0,
    createdTime: '2025-01-20 10:00:00',
    updatedTime: '2025-01-20 10:00:00'
  },
  {
    id: 2,
    dictName: '用户性别',
    dictType: 'user_gender',
    dictDesc: '用户性别字典，用于用户基本信息管理（男、女、未知）',
    status: 1,
    isSystem: 1,
    sortOrder: 2,
    createdBy: 1,
    updatedBy: 1,
    deleted: 0,
    createdTime: '2025-01-20 10:01:00',
    updatedTime: '2025-01-20 10:01:00'
  },
  
  // 系统内置字典类型 - 订单相关
  {
    id: 3,
    dictName: '订单状态',
    dictType: 'order_status',
    dictDesc: '订单状态字典，用于订单流程管理（待支付、已支付、配送中、已完成、已取消）',
    status: 1,
    isSystem: 1,
    sortOrder: 3,
    createdBy: 1,
    updatedBy: 1,
    deleted: 0,
    createdTime: '2025-01-20 10:02:00',
    updatedTime: '2025-01-20 10:02:00'
  },
  {
    id: 4,
    dictName: '支付方式',
    dictType: 'payment_method',
    dictDesc: '支付方式字典，用于支付渠道管理（微信支付、支付宝、银行卡、现金）',
    status: 1,
    isSystem: 1,
    sortOrder: 4,
    createdBy: 1,
    updatedBy: 1,
    deleted: 0,
    createdTime: '2025-01-20 10:03:00',
    updatedTime: '2025-01-20 10:03:00'
  },
  
  // 系统内置字典类型 - 系统相关
  {
    id: 5,
    dictName: '操作类型',
    dictType: 'operation_type',
    dictDesc: '系统操作类型字典，用于日志记录和权限控制（新增、修改、删除、查询、登录、登出）',
    status: 1,
    isSystem: 1,
    sortOrder: 5,
    createdBy: 1,
    updatedBy: 1,
    deleted: 0,
    createdTime: '2025-01-20 10:04:00',
    updatedTime: '2025-01-20 10:04:00'
  },
  {
    id: 6,
    dictName: '风险等级',
    dictType: 'risk_level',
    dictDesc: '操作风险等级字典，用于安全管控（低风险、中风险、高风险）',
    status: 1,
    isSystem: 1,
    sortOrder: 6,
    createdBy: 1,
    updatedBy: 1,
    deleted: 0,
    createdTime: '2025-01-20 10:05:00',
    updatedTime: '2025-01-20 10:05:00'
  },
  
  // 系统内置字典类型 - 层级字典
  {
    id: 7,
    dictName: '行政区划',
    dictType: 'region',
    dictDesc: '中国行政区划字典，支持省市区三级联动，用于地址管理',
    status: 1,
    isSystem: 1,
    sortOrder: 10,
    createdBy: 1,
    updatedBy: 1,
    deleted: 0,
    createdTime: '2025-01-20 10:06:00',
    updatedTime: '2025-01-20 10:06:00'
  },
  {
    id: 8,
    dictName: '行业分类',
    dictType: 'industry',
    dictDesc: '行业分类字典，支持多级分类（信息技术、金融服务、制造业等）',
    status: 1,
    isSystem: 1,
    sortOrder: 11,
    createdBy: 1,
    updatedBy: 1,
    deleted: 0,
    createdTime: '2025-01-20 10:07:00',
    updatedTime: '2025-01-20 10:07:00'
  },
  
  // 用户创建的字典类型
  {
    id: 9,
    dictName: '异常类型',
    dictType: 'error_type',
    dictDesc: '系统异常类型字典，用于错误分类和处理',
    status: 1,
    isSystem: 0,
    sortOrder: 20,
    createdBy: 1,
    updatedBy: 1,
    deleted: 0,
    createdTime: '2025-01-21 09:15:00',
    updatedTime: '2025-01-21 09:15:00'
  },
  {
    id: 10,
    dictName: '异常级别',
    dictType: 'error_level',
    dictDesc: '系统异常级别字典，用于异常严重程度分级（低、中、高、紧急）',
    status: 1,
    isSystem: 0,
    sortOrder: 21,
    createdBy: 1,
    updatedBy: 1,
    deleted: 0,
    createdTime: '2025-01-21 09:16:00',
    updatedTime: '2025-01-21 09:16:00'
  },
  {
    id: 11,
    dictName: '二维码类型',
    dictType: 'qrcode_type',
    dictDesc: '二维码类型字典，用于不同场景的二维码分类（支付码、会员码、商品码）',
    status: 0, // 禁用状态，用于测试状态筛选
    isSystem: 0,
    sortOrder: 30,
    createdBy: 1,
    updatedBy: 1,
    deleted: 0,
    createdTime: '2025-01-21 14:20:00',
    updatedTime: '2025-01-21 14:20:00'
  },
  {
    id: 12,
    dictName: '通用状态',
    dictType: 'common_status',
    dictDesc: '通用状态字典，适用于各种业务场景的状态管理（启用、禁用、草稿、审核中）',
    status: 1,
    isSystem: 0,
    sortOrder: 100,
    createdBy: 1,
    updatedBy: 1,
    deleted: 0,
    createdTime: '2025-01-22 11:30:00',
    updatedTime: '2025-01-22 11:30:00'
  }
];

// 获取字典类型列表数据
export function getMockSysDictTypeData(params: any = {}) {
  let filteredData = [...mockDictTypes];
  
  // 搜索过滤
  if (params.dictName) {
    filteredData = filteredData.filter(item => 
      item.dictName.includes(params.dictName)
    );
  }
  
  if (params.dictType) {
    filteredData = filteredData.filter(item => 
      item.dictType.includes(params.dictType)
    );
  }
  
  if (params.status !== undefined && params.status !== null) {
    filteredData = filteredData.filter(item => 
      item.status === params.status
    );
  }
  
  // 排序
  filteredData.sort((a, b) => a.sortOrder - b.sortOrder);
  
  // 分页
  const pageSize = params.pageSize || 10;
  const current = params.current || 1;
  const start = (current - 1) * pageSize;
  const end = start + pageSize;
  
  return {
    records: filteredData.slice(start, end),
    total: filteredData.length,
    current,
    pageSize
  };
}

// 获取字典类型详情
export function mockFetchSysDictTypeDetail(id: number) {
  const item = mockDictTypes.find(item => item.id === id);
  if (!item) {
    return { success: false, message: '字典类型不存在' };
  }
  return { success: true, data: item };
}

// 新增字典类型
export function mockAddSysDictType(data: Partial<SysDictType>) {
  // 检查字典类型标识是否已存在
  const exists = mockDictTypes.some(item => item.dictType === data.dictType);
  if (exists) {
    return { success: false, message: '字典类型标识已存在' };
  }
  
  const newId = Math.max(...mockDictTypes.map(item => item.id)) + 1;
  const now = new Date().toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  }).replace(/\//g, '-');
  
  const newItem: SysDictType = {
    id: newId,
    dictName: data.dictName || '',
    dictType: data.dictType || '',
    dictDesc: data.dictDesc || '',
    status: data.status || 1,
    isSystem: 0, // 用户创建的都不是系统内置
    sortOrder: data.sortOrder || 0,
    createdBy: 1,
    updatedBy: 1,
    deleted: 0,
    createdTime: now,
    updatedTime: now
  };
  
  mockDictTypes.push(newItem);
  return { success: true, data: newItem };
}

// 编辑字典类型
export function mockEditSysDictType(id: number, data: Partial<SysDictType>) {
  const index = mockDictTypes.findIndex(item => item.id === id);
  if (index === -1) {
    return { success: false, message: '字典类型不存在' };
  }
  
  const now = new Date().toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  }).replace(/\//g, '-');
  
  mockDictTypes[index] = {
    ...mockDictTypes[index],
    ...data,
    id, // 确保ID不被修改
    dictType: mockDictTypes[index].dictType, // 确保字典类型标识不被修改
    updatedTime: now
  };
  
  return { success: true, data: mockDictTypes[index] };
}

// 删除字典类型
export function mockDeleteSysDictType(id: number) {
  const index = mockDictTypes.findIndex(item => item.id === id);
  if (index === -1) {
    throw new Error('字典类型不存在');
  }
  
  if (mockDictTypes[index].isSystem === 1) {
    throw new Error('系统内置字典类型不允许删除');
  }
  
  mockDictTypes.splice(index, 1);
  return { success: true };
}

// 更新字典类型状态
export function mockUpdateSysDictTypeStatus(id: number, status: number) {
  const index = mockDictTypes.findIndex(item => item.id === id);
  if (index === -1) {
    throw new Error('字典类型不存在');
  }
  
  if (mockDictTypes[index].isSystem === 1) {
    throw new Error('系统内置字典类型不允许修改状态');
  }
  
  const now = new Date().toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  }).replace(/\//g, '-');
  
  mockDictTypes[index].status = status;
  mockDictTypes[index].updatedTime = now;
  
  return { data: mockDictTypes[index] };
}

// 更新字典类型排序
export function mockUpdateSysDictTypeSortOrder(id: number, sortOrder: number) {
  const index = mockDictTypes.findIndex(item => item.id === id);
  if (index === -1) {
    throw new Error('字典类型不存在');
  }
  
  const now = new Date().toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  }).replace(/\//g, '-');
  
  mockDictTypes[index].sortOrder = sortOrder;
  mockDictTypes[index].updatedTime = now;
  
  return { data: mockDictTypes[index] };
}

// 注意：这里不重新导出生成的Mock函数，避免重复声明
// 如果需要使用生成的Mock函数，请直接从 './auto/generated-mock' 导入